// pages/add_address/add_address.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    addressData:{
      type: 1, // 1添加   0修改
      name:"",
      tel:"",
      city:"",
      site:"",
      status:0
    }
  },
  nameIpt(e){
    this.setData({
      'addressData.name': e.detail.value
    })
  },
  telIpt(e) {
    this.setData({
      'addressData.tel': e.detail.value
    })
  },
  siteIpt(e) {
    this.setData({
      'addressData.site': e.detail.value
    })
  },
  bindRegionChange(e){
    this.setData({
      region: e.detail.value,
      'addressData.city': e.detail.value
    })
  },
  //保存信息
  addAddress: function () {
    let that = this;
    let addressData = that.data.addressData;
    let telReg = /^1[3-9]\d{9}$/;
    if (addressData.name == '') {
      app.showToast('收货人不能为空');
      return;
    }else if (addressData.tel == '') {
      app.showToast('电话号码不能为空')
      return;
    } else if (!telReg.test(addressData.tel)) {
      app.showToast('电话号码格式错误')
      return;
    }else if (addressData.city == "") {
      app.showToast('请选择所在城市')
      return;
    } else if (addressData.site == '') {
      app.showToast('详细地址不能为空')
      return;
    }else{
      wx.showLoading({
        title: '添加中...',
      })
      wx.request({
        url: app.buildUrl('/addr/add-address'),
        method: 'POST',
        data: {
          'id': that.data.id,
          'type': addressData.type,
          'name': addressData.name,
          'mobile': addressData.tel,
          'province': addressData.city[0],
          'city': addressData.city[1],
          'area': addressData.city[2],
          'district': addressData.site,
          'status': addressData.status
        },
        header: app.buildHeader(),
        success: function (res) {
          wx.hideLoading()
          if (res.data.code == 200){
            setTimeout(()=>{
              wx.navigateBack({
                delta:1
              })
            },1000)
          }
          app.showToast(res.data.msg)
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      id: options.id,
      'addressData.type': options.id > 0 ? 0 : 1,
      'addressData.status':options.status==1?1:0
    })
    //options.id -- 有就是修改 没有就是新加
    if (options.id){
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.buildUrl('/addr/get-address-info'),
        header: app.buildHeader(),
        data: { id: options.id},
        success(res){
          wx.hideLoading()
          if(res.data.code == 200){
            let data = res.data.data;
            let arr = [];
            arr[0] = data.province;
            arr[1] = data.city;
            arr[2] = data.area;
            that.setData({
              'addressData.name': data.name,
              'addressData.tel': data.mobile,
              'addressData.city': arr,
              'addressData.site': data.district
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  }
})